package cn.zs.supply.common.utils;
import cn.zs.supply.common.core.domain.vo.QrCodeVo;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URLEncoder;
import java.util.Date;
import java.util.Hashtable;
import java.util.List;
import java.util.zip.ZipOutputStream;
/***
 * 二维码生成下载展示工具类
 */
public class QrCodeUtils {
    Boolean download=false;//展示或下载
    String organZipName;//压缩包下载
    int width; int height; HttpServletResponse response;List<QrCodeVo> QrCodeVo;
    public   QrCodeUtils(boolean download,int width,int height, HttpServletResponse response,List<QrCodeVo> QrCodeVo){
        this.organZipName= DateUtil.formatDate(new Date(), "yyyyMMddHHmmss");
        this.download=download;
        this.width=width;
        this.height=height;
        this.response=response;
        this.QrCodeVo=QrCodeVo;
    }
    /**单个二维码下载或展示********/
    public  void creatRrCode() {
        Hashtable hints = new Hashtable();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        /**是否下载***/
        if(download) {
            try {
                response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(QrCodeVo.get(0).getName(), "UTF-8")+".png");
                response.setContentType("application/binary;charset=utf-8");
            }
            catch (Exception e){
               e.printStackTrace();
            }
        }
        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(QrCodeVo.get(0).getContents(), BarcodeFormat.QR_CODE, width, height, hints); // 1、读取文件转换为字节数组
            BufferedImage image = toBufferedImage(bitMatrix);
            ImageIO.write(image, "png", response.getOutputStream());
            response.getOutputStream().flush();
        } catch (WriterException e) { // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    /**多个二维码压缩包下载********/
    public  void creatRrCodeList() {
        Hashtable hints = new Hashtable();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        response.setHeader("Content-Disposition", "attachment;filename="+organZipName+".zip");
        response.setContentType("application/binary;charset=utf-8");
        try {
            ZipOutputStream zip = new ZipOutputStream(response.getOutputStream());
            InputStream in=null;
            for(QrCodeVo q:QrCodeVo) {
                BitMatrix bitMatrix = new MultiFormatWriter().encode(q.getContents(), BarcodeFormat.QR_CODE, width, height, hints); // 1、读取文件转换为字节数组
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                BufferedImage image = toBufferedImage(bitMatrix);
                /**转换成流*******/
                ImageIO.write(image, "png", out);
                byte[] a = out.toByteArray();
                in= new ByteArrayInputStream(a);
                ZipUtils.generateZipFile(in,zip,q.getName()+".png");
            }
                zip.close();
        } catch (WriterException e) { // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    /**
     * image流数据处理
     */
    public  BufferedImage toBufferedImage(BitMatrix matrix) {
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                image.setRGB(x, y, matrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
            }
        }
        return image;
    }
}